<!--
  @Description:新增
  @Author:zqy
  @Date:2023-07-02 15:41:29
  @Last Modified by:zqy
  @Last Modified time:2023-07-02 15:41:29
-->
<template>
  <el-dialog
    ref="formDlg"
    :title="`${!!(detail && detail.id)?'编辑':'新增'}`"
    :visible.sync="dialogShow"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :before-close="clearForm"
    width="550px"
    @close="closeDialog"
  >
    <div style="padding-right:20px">
      <el-form ref="refForm" :rules="formRules" :model="formData" size="mini" label-width="90px">
        <!-- <el-form-item label="时间限制:" prop="timeLimit">
          <el-date-picker
            v-model="formData.timeLimit"
            type="datetimerange"
            format="yyyy-MM-dd HH:mm:ss"
            value-format="yyyy-MM-dd HH:mm:ss"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
          />
        </el-form-item> -->
        <el-form-item label="时间限制:" prop="month">
          <el-date-picker
            v-model="formData.month"
            type="month"
            format="yyyy-MM"
            value-format="yyyy-MM"
            placeholder="请选择不可分享的上市日期"
            style="width:100%"
          />
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer">
      <el-button size="mini" @click="closeDialog">取消</el-button>
      <el-button v-loading="submiting" size="mini" type="primary" @click="submitForm">保存</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { saveConfig } from '@/api/shareConfig'
export default {
  name: 'ShareConfigAdd',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    detail: {
      type: [String, Object],
      default: ''
    }
  },
  data() {
    return {
      formData: {
        id: null,
        month: '',
        timeLimit: []
      },
      formRules: {
        month: [
          { required: true, message: '请选择不能分享的上市日期', trigger: 'change' }
        ]
        // timeLimit: [
        //   { required: true, message: '请选择时间限制', trigger: 'change' }
        // ]
      },
      submiting: false
    }
  },

  computed: {
    dialogShow: {
      get() {
        return this.visible
      },
      set(val) {
        this.$emit('update:visible', val)
      }
    }
  },
  created() {
    if (this.detail && this.detail.id) {
      const d = this.detail
      this.formData = {
        id: d.id,
        timeLimit: d.endTime ? [d.beginTime, d.endTime] : []
      }
    }
  },

  methods: {
    closeDialog() {
      this.dialogShow = false
      this.clearForm()
    },
    clearForm(done) {
      this.formData = {
        id: null,
        month: '',
        timeLimit: []
      }
      this.$refs.refForm && this.$refs.refForm.clearValidate()
      done && done()
    },
    submitForm(type) {
      if (this.submiting) return
      this.$refs['refForm'].validate(async(valid) => {
        if (valid) {
          const { timeLimit, id, month } = this.formData
          const arr = month.split('-')
          const beginTime = `${month}-01 00:00:00` // timeLimit[0]
          let endyear = Number(arr[0])
          let endmonth = Number(arr[1])
          if (endmonth === 12) {
            endyear += 1
            endmonth = 1
          } else {
            endmonth += 1
          }
          const endTime = `${endyear}-${endmonth < 10 ? '0' + endmonth : endmonth}-01 00:00:00` // timeLimit[1]

          const data = {
            beginTime,
            endTime,
            type: 'exclude'// （包含：include ，不包含：exclude ）
          }
          if (id) {
            data.id = id
          }

          this.submiting = true
          await saveConfig(data).then(res => {
            this.submiting = false
            if (res.success === 1) {
              this.$message({
                message: `${data.id ? '编辑' : '新增'}保存成功`,
                type: 'success'
              })
              this.$emit('success', !data.id)
              this.closeDialog()
            }
          }).catch(() => {
            this.submiting = false
          })
        }
      })
    }
  }
}
</script>
<style lang='scss' scoped>

</style>
